<template>
	<div class="app-container">
		<jk-operation-card ref="box1" class="margin-bottom-10">
			<div class="flex-between-center-wrap">
				<div class="flex-start-center-wrap">
					<el-checkbox v-model="isCustomTime" label="自定义查询时间" border size="small" @change="onCustomTimeChange"></el-checkbox>
				</div>
				<div class="flex-start-center">
					<jk-select v-model="searchParams.workshopId" clearable placeholder="区域" :option-list="workshopList" size="small" class="search-bar-item-size2 margin-5-0 item-gap"></jk-select>
					<jk-select v-model="searchParams.processCode" clearable placeholder="工序" value-key-name="code" :option-list="processList" size="small" class="search-bar-item-size1 margin-5-0 item-gap"></jk-select>
					<jk-date-picker v-show="!isCustomTime" v-model="searchParams.belongDate" type="date" :picker-options="pickerOptions" clearable placeholder="日期" size="small" class="search-bar-item-size2 margin-5-0 item-gap"></jk-date-picker>
					<jk-select v-show="!isCustomTime" v-model="searchParams.shiftId" clearable placeholder="班次" :option-list="shiftList" size="small" class="search-bar-item-size1 margin-5-0 item-gap"></jk-select>
					<jk-date-picker v-show="isCustomTime" v-model="searchParams.dateTimeRange" type="datetimerange" clearable start-placeholder="开始时间" end-placeholder="结束时间" size="small" style="width: 380px" class="margin-5-0 item-gap"></jk-date-picker>
					<jk-search-input v-model="searchParams.machineNameOrCode" placeholder="机台编号或名称" size="small" class="search-bar-item-size2 margin-5-0 item-gap" clearable @on-search="onSearchBtn" />
					<jk-search-button @click="onSearchBtn"></jk-search-button>
				</div>
			</div>
		</jk-operation-card>

		<jk-card>
			<jk-table ref="jkTable" border :loading="tableLoading" :data="tableData" :height="tableHeight + 20">
				<vxe-column
					v-for="(item, index) in tableColumnList"
					:key="index"
					:title="item.title"
					:align="item.align"
					:field="item.keyName"
					:sortable="item.sortable"
					:fixed="item.fixed ? item.fixed : ''"
					:min-width="item.minWidth"
					:width="item.width"
				>
					<template slot-scope="scope">
						<jk-state-bar v-if="item.keyName === 'typeId'" :state-list="typeList" :state="scope.row.typeId"></jk-state-bar>
						<span v-else-if="item.keyName === 'machineName'" style="cursor:pointer;color:#1890ff;" @click="cellHandleclick(scope)">{{ scope.row.machineName }}</span>
						<span v-else-if="item.keyName === 'machineStopDoffNumber'">{{ scope.row.machineStopDoffNumber || 0 }}次，{{ scope.row.machineStopDoffHour || 0 }}小时</span>
						<span v-else-if="item.keyName === 'machineStopChangeNumber'">{{ scope.row.machineStopChangeNumber || 0 }}次，{{ scope.row.machineStopChangeHour || 0 }}小时</span>
						<span v-else-if="item.keyName === 'machineStopErrorNumber'">{{ scope.row.machineStopErrorNumber || 0 }}次，{{ scope.row.machineStopErrorHour || 0 }}小时</span>
						<span v-else-if="item.keyName === 'machineStopOfflineNumber'">{{ scope.row.machineStopOfflineNumber || 0 }}次，{{ scope.row.machineStopOfflineHour || 0 }}小时</span>
						<span v-else-if="item.keyName === 'machineStopStandbyNumber'">{{ scope.row.machineStopStandbyNumber || 0 }}次，{{ scope.row.machineStopStandbyNumber || 0 }}小时</span>
						<span v-else>{{ scope.row[item.keyName] }}</span>
					</template>
				</vxe-column>
			</jk-table>

			<!-- 分页组件 -->
			<footer ref="box2" class="flex-end-center margin-top-10">
				<jk-page
					:page-index="searchParams.pageNo"
					:page-sizes="searchParams.pageSizes"
					:page-size="searchParams.pageSize"
					:total="totalCount"
					@on-change="onPageChange"
				/>
			</footer>
		</jk-card>
		<detail-dialog
			:machine="machine"
			:params="dataItem"
			:dialog-state="detailDialogState"
			@on-visible-change="onVisibleChangeOfDetailDialogState"
		></detail-dialog>
	</div>
</template>

<script>
    import calHeight from '/src/mixins/cal-table-height';
    import { pageConfig, formatDate } from '/src/utils/jk/jk-tool';
    import getMenuColumn from '../../../mixins/getMenuColumn';
    import handlerCache from '../../../mixins/handler-cache';
    import { productionMachineShutdownRecordsPage } from '../../../api/production/reports';
    import { workforcemanagementShiftPage } from '../../../api/schedule/shift';
    import detailDialog from './detail-dialog';
    import { getHaltanalyseEquipment } from '@/api/statistic/haltanalyse.js';

    export default {
        name: 'ShutdownAnalysisReport',
        components: {
            detailDialog
        },
        mixins: [calHeight, getMenuColumn, handlerCache],
        data() {
            return {
                apiItem: {
                    'page': getHaltanalyseEquipment
                },
                pickerOptions: {
                    disabledDate(time) {
                        return time.getTime() > Date.now();
                    }
                },
                dataItem: {},
                shiftList: [],
                processList: [],
                workshopList: [],
                machine: '',
                tableLoading: true,
                dataId: null,
                detailDialogState: false,
                tableData: [],
                totalCount: 0,
                searchParams: {
                    pageNo: pageConfig.pageNo,
                    pageSize: pageConfig.pageSize,
                    processCode: '',
                    workshopId: null,
                    shiftId: null,
                    typeId: '',
                    belongDate: '',
                    dateTimeRange: [],
                    machineNameOrCode: ''
                },
                isCustomTime: false
            };
        },
        mounted() {
            this.getDependentData();
        },
        methods: {
            onVisibleChangeOfDetailDialogState(e) {
                this.detailDialogState = e;
            },
            cellHandleclick(row) {
                this.machine = row.row.machineName;
                const machineNameOrCode = row.row.machineName;
                const typeId = row.row.typeId;
                const shutdownState = row.row.shutdownState;
                this.dataItem = { ...this.searchParams, machineNameOrCode, typeId, shutdownState };
                this.detailDialogState = true;
            },
            onVisibleChangeOfDetailDialog(e) {
                if (!e) {
                    this.dataId = null;
                }
                this.detailDialogState = e;
            },
            onCustomTimeChange(e) {
                if (e) {
                    this.$set(this.searchParams, 'shiftId', null);
                    this.$set(this.searchParams, 'belongDate', null);
                } else {
                    this.$set(this.searchParams, 'dateTimeRange', []);
                }
            },
            onPageChange(e, size) {
                this.$set(this.searchParams, 'pageNo', e);
                this.$set(this.searchParams, 'pageSize', size);
                this.getListRequest();
            },
            clearCacheMethod() {
                // Object.assign(this.$data, this.$options.data());
                this.$nextTick(() => {
                    this.getDependentData();
                });
            },
            getShiftListRequest() {
                return workforcemanagementShiftPage({ auditState: 1 }).then(res => {
                    if (res.data.status === 200) {
                        this.shiftList = res.data.res || [];
                        this.$set(this.searchParams, 'shiftId', this.shiftList && this.shiftList.length ? this.shiftList[0].id : null);
                    }
                });
            },
            async getDependentData() {
                this.$set(this.searchParams, 'belongDate', formatDate(Date.now()));
                this.$set(this.searchParams, 'processCode', this.$store.state.user.userProcess.defaultProcessCode);
                this.processList = this.$store.state.user.userProcess.processList ? this.$store.state.user.userProcess.processList.filter(x => !['QH', 'TB', 'EB', 'MB'].includes(x.code.toUpperCase())) : [];
                this.$set(this.searchParams, 'workshopId', this.$store.state.user.userWorkshop.defaultWorkshopId);
                this.workshopList = this.$store.state.user.userWorkshop.workshopList;

                this.tableLoading = true;
                await this.getMenuColumnListRequest();
                await this.getShiftListRequest();
                await this.getListRequest();
                this.tableLoading = false;
            },
            /** 查询列表 */
            getListRequest() {
                this.tableLoading = true;
                const params = JSON.parse(JSON.stringify(this.searchParams));
                if (params.dateTimeRange && params.dateTimeRange.length) {
                    params.beginTime = params.dateTimeRange[0];
                    params.endTime = params.dateTimeRange[1];
                }
                delete params.dateTimeRange;
                return this.apiItem['page'](params).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.tableData = res.data.res;
                        this.totalCount = res.data.count;
                    }
                });
            },
            /** 搜索按钮操作 */
            onSearchBtn() {
                this.searchParams.pageNo = 1;
                this.totalCount = 1;
                this.getListRequest();
            }
        }
    };
</script>
<style>
</style>
